<?php
/**
 * 商品分类控制器
 * Created by PhpStorm.
 * User: Jmashimaro
 * Date: 2015/2/9
 * Time: 12:14
 */
class CategoryApp extends MallbaseApp {

    private $common;

    public function __construct() {
        $this->common = new CommonApp();
    }


    /**
     * 获取分类
     * @author jyx
     * @date 2015/07/29
     */
    public function cateList() {
        $action = '获取分类';
        setLogParameter($action);
        $method = strtoupper($_SERVER['REQUEST_METHOD']);
        if ($method != 'GET') {
            $this->ajax_return(FLAG_ERROR ,'非法的请求！');
        }
        $model = m('gcategory');
        //获取一级分类和二级分类
        $data = array();
        $sql = "SELECT cate_id,cate_name,parent_id,cate_image FROM ".DB_PREFIX."gcategory WHERE parent_id=0 AND store_id=0 AND if_show=1
                ORDER BY sort_order DESC,cate_id ASC";
        $data = $model->getAll($sql);
        foreach($data as  $k=>$value){
            $data[$k]['big_cate_image'] = $value['cate_image'] ? QINIU_URL.$value['cate_image'].'_960_1470_2_85.jpg' : '';
            $data[$k]['small_cate_image'] = $value['cate_image'] ? QINIU_URL.$value['cate_image'].'_960_945_2_85.jpg' : '';
            $value['cate_image'] = $value['cate_image'] ? QINIU_URL.$value['cate_image'].'_960_498_2_85.jpg' : '';
            $data[$k]['cate_image']=$value['cate_image'];
        }
        $tmps = array(
            'cate_id' => '',
            'cate_name' => 'All',
            'parent_id' => '',
        );
        if ($data) {
            foreach ($data as $key=>$val) {
                $tmp = array();
                $sql = "SELECT cate_id,cate_name,parent_id FROM ".DB_PREFIX."gcategory WHERE parent_id={$val['cate_id']} AND store_id=0 AND if_show=1
                ORDER BY sort_order DESC,cate_id ASC";
                $tmp = $model->getAll($sql);
                $tmp = array_merge(array($tmps), $tmp);
                $val['sub'] = $tmp;
                $data[$key] = $val;
            }
        }
        $this->ajax_return(FLAG_OK, '', $data);
    }


    /**
     * 分类信息下的商品列表
     * @author jyx
     * @date 2015/07/29
     */
    public function goodsList() {
        $action = '分类信息下的商品列表';
        setLogParameter($action);
        $this->check_method('GET');
        $version = $_SERVER['HTTP_VERSION'];
        $user_id = $this->checkLogin();
        $offset     = $this->get_gp('offset', 'G');
        $size       = $this->get_gp('size', 'G');
        $cate_id1   = $this->get_gp('cate_id1', 'G');//一级分类
        $cate_id2   = $this->get_gp('cate_id2', 'G');//二级分类
        $cate_id1   = $cate_id1 ? intval($cate_id1) : 0;
        $cate_id2   = $cate_id2 ? intval($cate_id2) : 0;
        $offset     = $offset ? intval($offset) : 0;
        $size       = $size ? intval($size) : 10;
        $condition = $conditions = '';

        $conditions = $cate_id1 ? "cate_id={$cate_id1}" : "parent_id={$cate_id1}";

        $model = m('gcategory');
        $tmp = $data = array();
        //获取一级分类
        $sql = "SELECT cate_id,cate_name,parent_id FROM ".DB_PREFIX."gcategory WHERE $conditions AND store_id=0
                AND if_show=1 ORDER BY sort_order DESC,cate_id ASC Limit 1";
        $data = $model->getRow($sql);
        if ($data) {
            //获取二级分类
            $sql = "SELECT cate_id,cate_name,parent_id FROM ".DB_PREFIX."gcategory WHERE parent_id={$data['cate_id']}
            AND store_id=0 ORDER BY sort_order DESC,cate_id ASC";
            $tmp = $model->getAll($sql);
        }
        $data['sub'] = $tmp;
        if ($cate_id1) {
            $condition = " g.cate_id_1={$data['cate_id']} AND ";
        }
        if ($cate_id2) {
            $condition .= " g.cate_id_2={$cate_id2} AND ";
        }
        $goods = array();
        $sql = "SELECT g.goods_id,g.goods_name,g.price,g.default_image,g.currency,g.source,g.is_act,g.discount_price,g.act_start_time,
                g.act_end_time,gs.collects,gs.base_collects FROM ".DB_PREFIX."goods g LEFT JOIN ".DB_PREFIX."goods_statistics gs
                ON g.goods_id = gs.goods_id WHERE {$condition} g.if_examine = 1 AND g.if_finance = 1 AND g.if_show = 1
                AND g.closed = 0 ORDER BY g.goods_id DESC LIMIT {$offset},{$size}";
        $goods = $model->getAll($sql);
        if ($goods) {
            foreach ($goods as $key=>$val) {
                $val['collects'] = $val['collects'] + $val['base_collects'];
                $val['discount'] = $this->common->isAct($val);//是否是优惠商品
                $val['follow'] = $user_id ? $this->common->getRelationShip($user_id, $val['goods_id']) : 0;//是否关注
                $val['total_stock'] = $this->common->getStock($val['goods_id']);//总库存
                $val['default_image'] = $this->common->isMySelfImage($val['default_image'], GOODS_IMG);
                $goods[$key] = $val;
            }
        }
        $data['goods'] = $goods;

        $this->ajax_return(FLAG_OK, '', array($data));

    }





}
